package com.ly.mpayment.core.util;

import java.util.Iterator;
import java.util.Set;

import org.apache.http.Header;

import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;

public class CookieRedis {

	public static final String key = "kks_hf:";

	public static boolean addCookie(String phoneNum,String jsonkey,String jsonValue){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if( phoneNum != null && !phoneNum.isEmpty()){
			if(jedis.exists(phoneNum)){
				JSONObject cookie = JSONObject.parseObject(jedis.get(phoneNum));
				cookie.put(key+jsonkey, jsonValue);
				jedis.set(phoneNum, cookie.toString());
			}else{
				JSONObject json = new JSONObject();
				json.put(key+jsonkey, jsonValue);
				jedis.set(phoneNum, json.toString());
			}
			RedisUtil.returnBrokenResource(jedis);
			return true;
		}
		return false;
	}

	public static boolean rmCookie(String phoneNum,String jsonkey){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if( phoneNum != null && !phoneNum.isEmpty()){
			if(jedis.exists(key+phoneNum)){
				JSONObject cookie = JSONObject.parseObject(jedis.get(key+phoneNum));
				cookie.remove(jsonkey);
				RedisUtil.returnBrokenResource(jedis);
				return true;
			}
		}
		RedisUtil.returnBrokenResource(jedis);
		return false;
	}

	public static boolean rmAll(){
		Jedis jedis = RedisUtil.getInstance().getJedis();
//		System.out.println(jedis.flushDB());
		Set<String> keys = jedis.keys("*");
		Iterator<String> it=keys.iterator() ;
        while(it.hasNext()){
            String key = it.next();
            System.out.println(key);
        }
		RedisUtil.returnBrokenResource(jedis);
		return false;
	}

	public static boolean rmKey(String phoneNum){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if( phoneNum != null && !phoneNum.isEmpty()){
			if(jedis.exists(key+phoneNum)){
				jedis.del(key+phoneNum);
				RedisUtil.returnBrokenResource(jedis);
				return true;
			}
		}
		RedisUtil.returnBrokenResource(jedis);
		return false;
	}

	public static boolean addCookie(String phoneNum,String content){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		jedis.set(key+phoneNum, content);
		RedisUtil.returnBrokenResource(jedis);
		return true;
	}


	public static boolean addCookie(String phoneNum,Header []  headers){
		for (Header str : headers) {
			String temp[] = str.getValue().split(";");
			for( int i = 0 ; i < temp.length ; i++ ){
				switch (temp[i].split("=")[0].trim()) {
				case "CITY_INFO":
//					json.put(temp[i].split("=")[0].trim(), temp[i].split("=")[1]);
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "sendflag":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "cmccssotoken":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "CmLocation":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "CmProvid":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "JSESSIONID":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "collect_id":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "CaptchaCode":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "rdmdmd5":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "lgToken":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "is_login":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "c":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "verifyCode":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				case "key4IE89":
					addCookie(phoneNum, temp[i].split("=")[0].trim(),  temp[i].split("=")[1]);
					break;
				default:
					break;
				}
			}
		}
		return true;
	}

	/**
	 * 获取哦可以
	 * @param phoneNum
	 * @return
	 */
	public static String get(String keys){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		String temp = jedis.get(key+keys);
		RedisUtil.returnBrokenResource(jedis);
		return temp;
	}

	public static String getHttpCookie(String phoneNum){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis.exists(key+phoneNum)){
			JSONObject cookie = JSONObject.parseObject(jedis.get(key+phoneNum));
			StringBuffer sb = new StringBuffer();
			for(String str : cookie.keySet()){
				if(str.equals(Config.channelID)){
					continue;
				}
				if(str.equals("JSESSIONIDs")){
					continue;
				}
				if(str.equals(Config.saleCode)){
					continue;
				}

				sb.append(str);
				sb.append("=");
				sb.append(cookie.getString(str));
				sb.append(";");
			}
			RedisUtil.returnBrokenResource(jedis);
			return sb.toString().substring(0, sb.lastIndexOf(";"));
		}
		RedisUtil.returnBrokenResource(jedis);
		return null;
	}

	/**
	 * 获取哦可以
	 * @param phoneNum
	 * @return
	 */
	public static JSONObject getCookieJson(String phoneNum){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis.exists(key+phoneNum)){
			String temp = jedis.get(key+phoneNum);
			RedisUtil.returnBrokenResource(jedis);
			return JSONObject.parseObject(temp);
		}
		RedisUtil.returnBrokenResource(jedis);
		return null;
	}
	public static String getCookie(String phoneNum,String jsonKey){
		Jedis jedis = RedisUtil.getInstance().getJedis();
		try {
			if(jedis.exists(key+phoneNum)){
				JSONObject cookie = JSONObject.parseObject(jedis.get(key+phoneNum));
				RedisUtil.returnBrokenResource(jedis);
				return cookie.containsKey(jsonKey) ? cookie.getString(jsonKey) : null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			jedis = RedisUtil.getInstance().getJedis();
			RedisUtil.returnBrokenResource(jedis);
			return getCookie( phoneNum,jsonKey);
		}
		RedisUtil.returnBrokenResource(jedis);
		return null;
	}
/**
 * Cookie: lgToken=bc086178beb1468f83f021858084207f; CaptchaCode=DYPEwB; rdmdmd5=5F1FFD04698A1D5BF49DAF7A723C57F8; sendflag=20190714183742074969;
 *  cmccssotoken=c73a04ef60d447149b87e433d24186a2@.10086.cn; is_login=true; c=c73a04ef60d447149b87e433d24186a2; verifyCode=f3b273a641332f0118e9735f7d82f40f9b7b65ef; key4IE89=43979A4575AD90460984385EF3E36F1BDC3F5251EBFEE62E4B278FAD262CA8F51774036FDA47A61526BA901BFD858E15; CmLocation=280|280; CmProvid=sc; WT_FPC=id=201d49fa3690d7522c71563100581459:lv=1563100732905:ss=1563100581459; jsessionid-echd-cpt-cmcc-jt=CB802EDEF45CA4EEE582FD0BB08D5BB5
 * @param phoneNum
 * @return
 */
	public static String getSsoCheck(String phoneNum) {
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis.exists(key+phoneNum)){
			JSONObject cookie = JSONObject.parseObject(jedis.get(key+phoneNum));
			StringBuffer sb = new StringBuffer();
			for(String str : cookie.keySet()){
				if(str.equals(Config.channelID)){
					continue;
				}
				if(str.equals("c")){
					continue;
				}
				if(str.equals("CITY_INFO")){
					continue;
				}
				if(str.equals(Config.JSESSIONID)){
					continue;
				}
				if(str.equals("domain")){
					continue;
				}
				if(str.equals("JSESSIONIDs")){
					continue;
				}
				if(str.equals(Config.saleCode)){
					continue;
				}

				sb.append(str);
				sb.append("=");
				sb.append(cookie.getString(str));
				sb.append(";");
			}
			RedisUtil.returnBrokenResource(jedis);
			return sb.toString().concat("CmLocation=280|280;CmProvid=sc");
		}
		RedisUtil.returnBrokenResource(jedis);
		return null;
	}

	public static String getauthcheck(String phoneNum) {
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis.exists(key+phoneNum)){
			JSONObject cookie = JSONObject.parseObject(jedis.get(key+phoneNum));
			StringBuffer sb = new StringBuffer();
			for(String str : cookie.keySet()){
				if(str.equals("channelID")){
					continue;
				}
				if(str.equals("c")){
					continue;
				}
				if(str.equals("CITY_INFO")){
					continue;
				}
				if(str.equals("JSESSIONID")){
					continue;
				}
				if(str.equals("domain")){
					continue;
				}
				if(str.equals("JSESSIONIDs")){
					continue;
				}
				sb.append(str);
				sb.append("=");
				sb.append(cookie.getString(str));
				sb.append(";");
			}
			RedisUtil.returnBrokenResource(jedis);
			return sb.toString().concat("CmLocation=280|280;CmProvid=sc");
		}
		RedisUtil.returnBrokenResource(jedis);
		return null;
	}
}
